/*
 * Copyright (c) 2025 Sailing
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */
#include <stdint.h>

/* 使用该驱动，仅需修改以上NAND参数 */

#define BLOCK_ERASE_CMD         0xD8
#define READ_ID_CMD             0x9F
#define SET_FEATURE_CMD         0x1F
#define GET_FEATURE_CMD         0x0F                              
#define READ_TO_CACHE_CMD       0x13
#define READ_FROM_CACHE_CMD     0x03                              
  
#define ADDR_REG_PROTECTION     0xA0
#define ADDR_REG_FEATURE        0xB0
#define ADDR_REG_STATUS         0xC0

/*
 * FEATURE_OPT_EN (RW)
 *
 * Enable OTP Can be Read
 */
#define FEATURE_OPT_EN_MASK (0x80UL)
#define FEATURE_OPT_EN_SHIFT (7U)
#define FEATURE_OPT_EN_SET(x) (((uint32_t)(x) << FEATURE_OPT_EN_SHIFT) & FEATURE_OPT_EN_MASK)
#define FEATURE_OPT_EN_GET(x) (((uint32_t)(x) & FEATURE_OPT_EN_MASK) >> FEATURE_OPT_EN_SHIFT) 

typedef struct _jedec_nand{
   uint8_t (*spi_function)(uint8_t* write_buf,uint8_t* read_buf,uint32_t write_count,uint32_t read_count,uint8_t dummy_count);
   uint16_t Data_Area_Size;
   uint16_t Spare_Area_Size;
   uint16_t Block_Page_Num;
   uint16_t Block_Total_Num;
   uint8_t  lun_num;
}jedec_nand_handle_t;

#pragma pack(1) /* 对齐方式单字节对齐 */
typedef struct _Nand_Parameter_Page{
    uint32_t Parameter_Page_Signature;
    uint16_t Revision_Number;
    uint16_t Features_supported;
    uint8_t  Reserved_1[24];
    char     Device_Manufacturer[12];
    char     Device_Model[20];
    char     JEDEC_ID;
    uint16_t  Date_Code_1;
    uint8_t  Reserved_2[13];
    uint32_t  Page_Data_Size;
    uint16_t Page_Spare_Size;/* 前面都是对的 */
    uint32_t Partial_Page_Data_Size;
    uint16_t Partial_Page_Spare_Size;
    uint32_t Page_Num_Per_Block;
    uint32_t Block_Num_Per_Lun;
    uint8_t  Lun_Num;
    uint8_t  Reserved_3;
    uint8_t  Num_Bit_Per_Cell;
    uint16_t Bad_Block_Maximum;
    uint16_t Block_Endurance;
    uint8_t  Guaranteed_Valid_Block_Of_Target;
    uint16_t Block_Endurance_for_Guranteed_Valid_Block;
    uint8_t  Number_Of_Programs_Per_Page;
    uint8_t  Partial_Programming_Attributes;
    uint8_t  Number_Of_Bits_Ecc_Correctablility;
    uint8_t  Number_Of_Interleaved_Address_Bits;
    uint8_t  Interleaved_Operation_Attributes;
    uint8_t  Reserved_4[13];
    uint8_t  IO_Capacitance;
    uint16_t IO_Clock_Support;
    uint16_t Reserved_5;
    uint16_t tPROG;
    uint16_t tBERS;
    uint16_t tR;
    uint16_t Reserved_6;
    uint8_t  Reserved_7[23];
    uint16_t Vendor_Revision_number;
    uint8_t Vendor_specifilc[88];
    uint16_t Integrity_Crc;
    uint8_t  Value_of_Byte[512];
}Nand_Parameter_Page_t;



extern jedec_nand_handle_t nand_handle;

uint8_t init_jedec_nand_handle(jedec_nand_handle_t *jedec_handle , void* spi_function);
